package com.sinochem.yunlian.ship.base.biz;

import com.sinochem.yunlian.ship.api.vo.uc.UcUserInfo;
import com.sinochem.yunlian.ship.base.entity.BsFlickerActive;
import com.sinochem.yunlian.ship.base.mapper.BsFlickerActiveMapper;
import com.sinochem.yunlian.ship.base.vo.request.FlickerScreenVo;
import com.sinochem.yunlian.ship.common.biz.BaseBiz;
import com.sinochem.yunlian.ship.common.enums.ResponseCode;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinochem.yunlian.ship.base.entity.BsFlickerScreen;
import com.sinochem.yunlian.ship.base.mapper.BsFlickerScreenMapper;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 闪屏图片表
 *
 * @author ship-admin
 * @email shipadmin@sinochem.com
 * @date 2018-05-16 19:28:44
 */
@Service
public class BsFlickerScreenBiz extends BaseBiz<BsFlickerScreenMapper,BsFlickerScreen> {

    @Autowired
    private BsFlickerActiveMapper activeMapper;

    /**
     * 上传闪屏图片
     * 如果已经上传过就做修改操作
     * @param flickerScreenVos
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResponse flickerScreenAdd(List<FlickerScreenVo> flickerScreenVos, UcUserInfo ucUserInfo) {
        List<BsFlickerScreen> flickerScreens = new ArrayList<>();
        //验证参数
        for (FlickerScreenVo flickerScreenVo : flickerScreenVos) {
            if (StringUtils.isBlank(flickerScreenVo.getImgUrl())) {
                return new RestResponse(ResponseCode.INVALID_PARAM);
            }
            if (StringUtils.isBlank(flickerScreenVo.getDisplaySize())) {
                return new RestResponse(ResponseCode.INVALID_PARAM);
            }
            if (flickerScreenVo.getMobileTerminal() == null) {
                return new RestResponse(ResponseCode.INVALID_PARAM);
            }
            BsFlickerScreen flickerScreen = new BsFlickerScreen();
            BeanUtils.copyProperties(flickerScreenVo, flickerScreen);
            flickerScreens.add(flickerScreen);
        }

        //没有新增、有则修改
        for (BsFlickerScreen flickerScreen : flickerScreens) {
            BsFlickerScreen dbFlickerScreen = this.getBsFlickerScreen(flickerScreen);
            if(dbFlickerScreen==null||dbFlickerScreen.getId()==null){
                flickerScreen.setCreateTime(new Date());
                flickerScreen.setCreateBy(ucUserInfo.getId());
                this.insertSelective(flickerScreen);
            }else{
                flickerScreen.setId(dbFlickerScreen.getId());
                flickerScreen.setUpdateTime(new Date());
                flickerScreen.setUpdateBy(ucUserInfo.getId());
                this.updateSelectiveById(flickerScreen);
            }
        }
        return RestResponse.success();
    }

    public BsFlickerScreen getBsFlickerScreen(BsFlickerScreen flickerScreen){
        BsFlickerScreen uniScreen = new BsFlickerScreen();
        uniScreen.setMobileTerminal(flickerScreen.getMobileTerminal());
        uniScreen.setDisplaySize(flickerScreen.getDisplaySize());
        return  this.selectOne(uniScreen);
    }

    /**
     * 不存在返回默认图
     * @param flickerScreen
     * @return
     */
    public BsFlickerScreen getBsFlickerScreenOrDefault(BsFlickerScreen flickerScreen){
        BsFlickerScreen uniScreen = new BsFlickerScreen();
        uniScreen.setMobileTerminal(flickerScreen.getMobileTerminal());
        uniScreen.setDisplaySize(flickerScreen.getDisplaySize());
        BsFlickerScreen  screen =  this.selectOne(uniScreen);
        if(screen==null || screen.getId()==null){
            uniScreen.setDefaultImg(1);
            uniScreen.setDisplaySize(null);
            List<BsFlickerScreen> list = this.selectList(uniScreen);
            if(list!=null && list.size()>0){
                screen = list.get(0);
            }
        }
        return screen;
    }

    public BsFlickerScreen getBsFlickerScreen(FlickerScreenVo flickerScreenVo){
        BsFlickerScreen uniScreen = new BsFlickerScreen();
        uniScreen.setMobileTerminal(flickerScreenVo.getMobileTerminal());
        uniScreen.setDisplaySize(flickerScreenVo.getDisplaySize());
        return  this.selectOne(uniScreen);
    }


    public List<BsFlickerScreen> getBsFlickerScreenListByActiveId(Integer activeId) {
        BsFlickerScreen screen = new BsFlickerScreen();
        screen.setActiveId(activeId);
        return this.selectList(screen);
    }

    public void flickerScreenAddList(List<BsFlickerScreen> bsFlickerScreens){
        for (BsFlickerScreen bsFlickerScreen :bsFlickerScreens) {
            this.insertSelective(bsFlickerScreen);
        }
    }

    public void flickerScreenDelAll(){
        this.delete(null);
    }
}